package homework;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;

//作业标题(2717)
//最小的k个数
//
//作业内容
//最小的k个数
//https://leetcode.cn/problems/smallest-k-lcci/description/
public class h2717 {
    public int[] smallestK(int[] arr, int k) {
        int[] vec = new int[k];
        if(k == 0) return vec;
        PriorityQueue<Integer>  queue = new PriorityQueue<Integer>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        for(int a : arr)
        {
            if(queue.size() < k)
            {
                queue.add(a);
            }
            else
            {
                if(queue.peek() > a)
                {
                    queue.poll();
                    queue.offer(a);
                }
            }
        }
        for(int i = 0; i < k; i++)
        {
            vec[i] = queue.poll();
        }
        return vec;
    }
}
